package com.yzmoney.common.web.springmvc.interceptor;

import com.yzmoney.common.util.IDUtils;
import com.yzmoney.common.constant.PropertyUtils;
import com.yzmoney.common.web.util.WebUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by LiuJun on 2015/7/17.
 */
public class CommonInterceptor extends HandlerInterceptorAdapter {
    private static final Log log = LogFactory.getLog(CommonInterceptor.class);

    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        return true;
    }

    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        String cookieId = PropertyUtils.getCookieId();
        String value = WebUtils.getCookie(request, cookieId);
        value = StringUtils.trim(value);
        if (value == null || value.length() == 0) {
            String id = IDUtils.getObjectId();
            WebUtils.addCookie(request, response, PropertyUtils.getCookieId(), id);
        }
    }
}
